﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapICC_WFEVENTDEF(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveICC_WFEVENTDEF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            Result = QueryICC_WFEVENTDEF(dwData);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapICC_WFEVENTDEF(ref dwLocal, dwData);

            if (!flgExist)
            {
                dwLocal["ROWNID"] = GetSeq();
                InsertICC_WFEVENTDEF(dwLocal, ITXDBVERSION);
            }
            UpdateICC_WFEVENTDEF(dwLocal);
        }
        public void InsertICC_WFEVENTDEF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "50.24":
                    or1.CommandText = "INSERT INTO ICC_WFEVENTDEF(MANDANT,ROWNID,WFEVENTS,WFEDES,WFEHANDLER,NEWUSER,NEWTIME,CHGUSER,CHGTIME)"
                                + " VALUES (:MANDANT,:ROWNID,:WFEVENTS,:WFEDES,:WFEHANDLER,:NEWUSER,:NEWTIME,:CHGUSER,:CHGTIME)";
                    break;
                default:
                    break;
            }

            or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
            or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString();
            or1.Parameters.Add("WFEVENTS", OracleType.NVarChar).Value = dwData["WFEVENTS"].ToString();
            or1.Parameters.Add("WFEDES", OracleType.NVarChar).Value = dwData["WFEDES"].ToString();
            or1.Parameters.Add("WFEHANDLER", OracleType.NVarChar).Value = dwData["WFEHANDLER"].ToString();
            
            or1.Parameters.Add("NEWUSER", OracleType.NVarChar).Value = dwData["NEWUSER"].ToString();
            or1.Parameters.Add("NEWTIME", OracleType.NVarChar).Value = dwData["NEWTIME"].ToString();
            or1.Parameters.Add("CHGUSER", OracleType.NVarChar).Value = dwData["CHGUSER"].ToString();
            or1.Parameters.Add("CHGTIME", OracleType.NVarChar).Value = dwData["CHGTIME"].ToString();
            Connect2Proxy(or1);
        }
 
        public DataSet QueryICC_WFEVENTDEF(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "SELECT * FROM ICC_WFEVENTDEF WHERE  ROWNID=:ROWNID"; ; 
                    or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString();
                    break;
                case 2:
                    or1.CommandText = "SELECT * FROM ICC_WFEVENTDEF WHERE  MANDANT=:MANDANT AND WFCLASS=:WFCLASS"; ;
                    or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
                    or1.Parameters.Add("WFCLASS", OracleType.NVarChar).Value = dwData["WFCLASS"].ToString();
                    break;
                case 3:
                    or1.CommandText = "SELECT DISTINCT E.* FROM ICC_WFEVENTDEF E LEFT JOIN ICC_WFSUBSCRIBER S"
                        +" ON E.MANDANT=S.MANDANT AND E.WFEVENTS=S.WFEVENTS"
                        + " WHERE S.MANDANT=:MANDANT AND S.WFEUSER=:WFEUSER"; 
                    or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
                    or1.Parameters.Add("WFEUSER", OracleType.NVarChar).Value = dwData["WFEUSER"].ToString();
                    break;
                case 4:
                    or1.CommandText = "SELECT * FROM ICC_WFEVENTDEF WHERE  MANDANT=:MANDANT"; ;
                    or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
                    break;
                case 9999:
                    or1.CommandText = "SELECT * FROM ICC_WFEVENTDEF"; ;
         
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "ICC_WFEVENTDEF";
            Return = Result;
            return Return;
        }
        public void DeleteICC_WFEVENTDEF(DataRow dwData, int option = 1)
        {
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM ICC_WFEVENTDEF WHERE  ROWNID=:ROWNID"; ;
                    or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString();
                    break;
            }
            Connect2Proxy(or1);
        }
        public void UpdateICC_WFEVENTDEF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            #region====Command====
            or1.CommandText = "UPDATE ICC_WFEVENTDEF SET "
                +"MANDANT=:MANDANT,"
                +"ROWNID=:ROWNID,"
                +"WFTYPE=:WFTYPE,"
                +"WFCLASS=:WFCLASS,"
                +"WFEVENTS=:WFEVENTS,"
                +"WFEDES=:WFEDES,"
                +"NEWUSER=:NEWUSER,"
                +"NEWTIME=:NEWTIME,"
                +"CHGUSER=:CHGUSER,"
                +"CHGTIME=:CHGTIME,"
                +"WFEHANDLER=:WFEHANDLER "
                + " WHERE  "
                + "    ROWNID=:ROWNID ";
            #endregion
            #region====Parameters==== 
             or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
             or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString();
             or1.Parameters.Add("WFTYPE", OracleType.NVarChar).Value = dwData["WFTYPE"].ToString();
             or1.Parameters.Add("WFCLASS", OracleType.NVarChar).Value = dwData["WFCLASS"].ToString();
             or1.Parameters.Add("WFEVENTS", OracleType.NVarChar).Value = dwData["WFEVENTS"].ToString();
             or1.Parameters.Add("WFEDES", OracleType.NVarChar).Value = dwData["WFEDES"].ToString();
             or1.Parameters.Add("NEWUSER", OracleType.NVarChar).Value = dwData["NEWUSER"].ToString();
             or1.Parameters.Add("NEWTIME", OracleType.NVarChar).Value = dwData["NEWTIME"].ToString();
             or1.Parameters.Add("CHGUSER", OracleType.NVarChar).Value = dwData["CHGUSER"].ToString();
             or1.Parameters.Add("CHGTIME", OracleType.NVarChar).Value = dwData["CHGTIME"].ToString();
             or1.Parameters.Add("WFEHANDLER", OracleType.NVarChar).Value = dwData["WFEHANDLER"].ToString();         
            #endregion
            Connect2Proxy(or1);
        }
    }
}
